
// const { reject } = require("lodash");

// //  tyarn webpack-dev-server '.\lesson6 同步异步\index.js' --open
function ajax(url){
    return new Promise(function(resolve,reject){
        const xhr = new XMLHttpRequest();
        xhr.open('GET',url)
        xhr.responseType ='json'
        xhr.onload = function(){
            if(this.status === 200){
                resolve(this.response)
            }else{
                reject(new Error(this.statusText))
            }
        }
        xhr.send()
    })
}

// ajax('lesson6 同步异步/user.json').then(function(res){
//     console.log(123123,res)
//     return '123123'
// }).catch(function(err){
//     console.log(333333,err)
// }).finally(function(){
//     console.log('不管成功失败都走这里')
// })

// Promise.resolve(2)
// .then(3)
// .then(res=>Promise.resolve(res*2))
// .then(res=>res+4)
// .then(console.log)

// function *add(){
//     yield 1;
//     return 2;
// }

// let it = add();
// console.log( it.next() ); // { value:1, done:false }
// console.log( it.next() ); // { value:2, done:true }
// console.log( it.next() ); // { value:2, done:true }

// function *takename(){
//     let name = yield 'hanzhk'
//     console.log(name)
//     yield name+'-aaa'
// }

// const name = takename()
// console.log(name.next())
// console.log(name.next('xixix'))
// console.log(name.next('didid'))

// function * show() {
//     console.log('111:')
//     var a = yield
//     console.log('aaa:',a)
//     let b = yield a + 111
//     console.log('bbb',b)
//     yield 1
// }

// var gen = show()
// console.log(gen.next(1111))
// console.log(gen.next(1111))
// console.log(gen.next('xixixi'))

// 同步的方式写异步代码

// 例子
// let names = getnames('urls');
// let products = getproducts('purls')
// let avatars = getproducts('avatars')

function *main (){
    const names = yield ajax('lesson6 同步异步/user.json')
    console.log('innernames: ', names);
    const products = yield ajax('lesson6 同步异步/url.json')
    console.log('innerproducts: ', products);
    const avatars = yield ajax('lesson6 同步异步/user.json')
    console.log('inneravatars: ', avatars);
}

// console.log(resultFn.next().value.then((res)=>console.log('names',res)))
// console.log(resultFn.next().value.then((res)=>console.log('products',res)))
// console.log(resultFn.next().value.then((res)=>console.log('avatars',res)))

const resultFn = main()

function co(generator){
    const g = generator();
    function handleNext(result){
        if(result.done)return 
        result.value.then(function(res){
            handleNext(g.next(res))
        })
    }
    handleNext(g.next())
}

co(main)

async function main1 (){
    try{
        const names = await ajax('lesson6 同步异步/user.json')
        console.log('awaitnames: ', names);
        const products = await ajax('lesson6 同步异步/url.json')
        console.log('awaitproducts: ', products);
        const avatars = await ajax('lesson6 同步异步/user2.json')
        console.log('awaitavatars: ', avatars);
    } catch( err ){
        console.log( err )
    }
}

main1().then(()=>{
    console.log('all 执行完了')
})
